package com.example.treeHole.service.impl;

import com.example.treeHole.dao.ArticleMainMapper;
import com.example.treeHole.dao.ArticleReplyMapper;
import com.example.treeHole.dao.NotificationMainMapper;
import com.example.treeHole.service.ArticleReplyService;
import com.example.treeHole.utils.BaseContext;
import com.example.treeHole.utils.Uuid;
import com.example.treeHole.vo.ArticleReply;
import com.example.treeHole.vo.NotificationMain;
import com.example.treeHole.vo.dto.ArticleReplyDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleReplyServiceImpl implements ArticleReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private NotificationMainMapper notificationMainMapper;
    @Override
    public void create(ArticleReply articleReply) {
        articleReply.setId(Uuid.getUuID());
        articleReply.setCreateBy(BaseContext.getCurrentId());
        articleReply.setCreateDate(new Date());
        //todo 消息通知
        //回复的时候新建通知
        NotificationMain notificationMain =new NotificationMain();
        //isReply
        // 回复的状态
        //1：表示回复文章；
        //2：表示回复文章的回复；
        //3: 表示回复文章的回复的回复
        notificationMain.setId(Uuid.getUuID());
        notificationMain.setArticleId(articleReply.getArticleId());
        notificationMain.setSendId(BaseContext.getCurrentId());
        notificationMain.setCreateDate(new Date());
        notificationMain.setCreateBy(BaseContext.getCurrentId());
        //对个人
        notificationMain.setNotificationType("0");
        if ("1".equals(articleReply.getIsReply())){
            notificationMain.setNotificationCategory("1");
        }else {
            notificationMain.setNotificationCategory("2");
        }
        //未处理
        notificationMain.setNotificationStatus("0");
        notificationMain.setSendArticleId(articleReply.getId());
        notificationMainMapper.insert(notificationMain);
        articleReplyMapper.insert(articleReply);
    }

    @Override
    public ArticleReply findById(String id) {
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(id);
        return articleReply;
    }

    @Override
    public PageInfo<ArticleReply> page(ArticleReplyDto articleReplyDto) {
        PageHelper.startPage(articleReplyDto.getPageNum(),articleReplyDto.getPageSize());
        List<ArticleReply> articleReplies=articleReplyMapper.findAll();
        PageInfo<ArticleReply> of = PageInfo.of(articleReplies);
        return of;
    }
}
